<?php if (!defined('NOVA_RUN')) die('You can\'t access this file directly!');

/*
-------------------------------------------------------
*
*   NovaBoard 2
*   Copyright © 2011 NovaBoard team
*
*--------------------------------------------------------
*
*   Official site: www.novaboard.net
*   Contact e-mail: support@novaboard.net
*
*   GNU General Public License, version 2:
*   http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
---------------------------------------------------------
*/

class Register_Action extends Action
{
	// current action name
	var $action = 'register';

	// avaliable methods
	var $methods = array('view', 'activate');

	// default method
	var $default = 'view';

	public function view()
	{
		if ($this->curUser->info['user_id'] > 0)
		{
			Engine::showError(1);
		}

		$err = null;
		$success = null;
		
		if (get_request('register', 'post') == 1)
		{
			Engine::secretKey(true);
			if (!get_request('login', 'post') || !get_request('email', 'post') || !get_request('password', 'post'))
			{
				$err = lang::instance()->get('register', 'empty_fields');
			}
			elseif (!check_request(get_request('login', 'post'), 'string', Config::get('min_login_length'), Config::get('max_login_length')))
			{
				$err = sprintf(lang::instance()->get('register', 'wrong_login_length'), Config::get('min_login_length'), Config::get('max_login_length'));
			}
			elseif ($this->UserModel->checkLoginExists(get_request('login', 'post')) == 1)
			{
				$err = str_replace('%s', get_request('login', 'post'), lang::instance()->get('register', 'login_already_used'));
			}
			elseif (!check_request(get_request('email', 'post'), 'email'))
			{
				$err = lang::instance()->get('register', 'wrong_email');
			}
			elseif ($this->UserModel->checkEmailExists(get_request('email', 'post')) == 1)
			{
				$err = str_replace('%s', get_request('email', 'post'), lang::instance()->get('register', 'email_already_used'));
			}
			elseif (!check_request(get_request('password', 'post'), 'string', Config::get('min_password_length'), Config::get('max_password_length')))
			{
				$err = sprintf(lang::instance()->get('register', 'wrong_password_length'), Config::get('min_password_length'), Config::get('max_password_length'));
			}
			elseif (get_request('password', 'post') != get_request('repeat_password', 'post'))
			{
				$err = lang::instance()->get('register', 'passwords_dont_match');
			}
			else
			{
				$this->UserModel->register();
				$success = lang::instance()->get('register', 'success');
				unset($_POST);
			}
		}

		tpl::instance()->assign('err', $err);
		tpl::instance()->assign('success', $success);
		tpl::instance()->show('register');
	}

	public function activate()
	{
		if (is_numeric(Router::param(0)) && check_request(Router::param(1), 'md5'))
		{
			if (!$this->UserModel->activate(Router::param(0), Router::param(1)))
			{
				// code is wrong
				Engine::ShowError(5);
			}
			else
			{
				tpl::instance()->assign('success', lang::instance()->get('register', 'activated'));
				tpl::instance()->show('activate');
			}
		}
		else
		{
			// code is wrong
			Engine::ShowError(5);
		}
	}
}